package com.crcode.crm.service.system.impl;

import com.crcode.crm.bean.system.NoticeBean;
import com.crcode.crm.dao.system.NoticeDao;
import com.crcode.crm.service.system.NoticeService;
import com.crcode.crm.utils.PageVo;
import com.crcode.crm.utils.ResultPageUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * 公告业务实现层
 * @author xiangfei
 * @date 2019年3月25日 下午1:56:43
 * @version V1.0
 */
@Service(value="noticeService")
@Transactional
public class NoticeServiceImpl implements NoticeService {

    @Autowired
    private NoticeDao noticeDao;

    @Override
    public List<NoticeBean> findByIdIn(List<Integer> ids) {
        return this.noticeDao.findByIdIn(ids);
    }

    @Override
    public ResultPageUtil findAll(NoticeBean bean, PageVo vo) {
        Integer pageIndex = vo.getPageIndex() - 1;
        Pageable pageable = null;
        if(StringUtils.isNotBlank(vo.getOrderBy())){
            if(vo.getOrderDesc() == 1){
                pageable = PageRequest.of(pageIndex, vo.getPageSize(), Sort.Direction.DESC, vo.getOrderBy());
            }else if(vo.getOrderDesc() == 0){
                pageable = PageRequest.of(pageIndex, vo.getPageSize(), Sort.Direction.ASC, vo.getOrderBy());
            }
        }else{
            pageable = PageRequest.of(pageIndex, vo.getPageSize());
        }
        Page<NoticeBean> page = noticeDao.findAll(spec(bean), pageable);
        ResultPageUtil rpu = new ResultPageUtil();
        rpu.setCurrentPage(vo.getPageIndex());
        rpu.setPageSize(vo.getPageSize());
        rpu.setTotalPages(page.getTotalPages());
        rpu.setRows(page.getContent());
        rpu.setTotal(page.getTotalElements());
        return rpu;
    }

    /**
     * 创建动态查询条件组合
     * @date 2018年8月27日 下午2:59:37
     * @param notice
     * @return
     */
    private Specification<NoticeBean> spec(final NoticeBean notice) {
        Specification<NoticeBean> spec = new Specification<NoticeBean>() {

            private static final long serialVersionUID = 1L;

            @Override
            public Predicate toPredicate(Root<NoticeBean> root, CriteriaQuery<?> criteriaQuery,
                                         CriteriaBuilder cb) {
                List<Predicate> predicates = new ArrayList<>();
                if (StringUtils.isNotBlank(notice.getNoticeTitle())) {
                    predicates.add(cb.or(cb.like(root.get("noticeTitle"), "%" + notice.getNoticeTitle() + "%")));
                }
                return cb.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return spec;
    }

    @Override
    public ResultPageUtil findByNoticeTitleContaining(String search, PageVo vo) {
        Integer pageIndex = vo.getPageIndex() - 1;
        Pageable pageable = null;
        if(StringUtils.isNotBlank(vo.getOrderBy())){
            if(vo.getOrderDesc() == 0){
                pageable = PageRequest.of(pageIndex, vo.getPageSize(), Sort.Direction.ASC, vo.getOrderBy());
            }else if(vo.getOrderDesc() == 1){
                pageable = PageRequest.of(pageIndex, vo.getPageSize(), Sort.Direction.DESC, vo.getOrderBy());
            }
        }else{
            pageable = PageRequest.of(pageIndex, vo.getPageSize());
        }
        Page<NoticeBean> page = noticeDao.findByNoticeTitleContaining(search, pageable);
        ResultPageUtil resultPageUtil = new ResultPageUtil();
        resultPageUtil.setCurrentPage(vo.getPageIndex());
        resultPageUtil.setPageSize(vo.getPageSize());
        resultPageUtil.setTotalPages(page.getTotalPages());
        resultPageUtil.setRows(page.getContent());
        resultPageUtil.setTotal(page.getTotalElements());
        return resultPageUtil;
    }

    @Override
    public boolean deleteByIdIn(List<Integer> ids) {
        try {
            this.noticeDao.deleteByIdIn(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteByUserUuidIn(List<String> uuids) {
        try {
            this.noticeDao.deleteByUserUuidIn(uuids);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public List<NoticeBean> findByUserUuidIn(List<String> uuids) {
        return this.noticeDao.findByUserUuidIn(uuids);
    }

    @Override
    public NoticeBean getById(Integer integer) {
        return this.noticeDao.findById(integer).orElse(null);
    }

    @Override
    public NoticeBean saveResultBean(NoticeBean noticeBean) {
        return this.noticeDao.save(noticeBean);
    }

    @Override
    public boolean saveResultBoolean(NoticeBean noticeBean) {
        try {
            this.noticeDao.save(noticeBean);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public NoticeBean updateResultBean(NoticeBean noticeBean) {
        return this.noticeDao.save(noticeBean);
    }

    @Override
    public boolean updateResultBoolean(NoticeBean noticeBean) {
        try {
            this.noticeDao.save(noticeBean);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public boolean delete(Integer integer) {
        try {
            this.noticeDao.deleteById(integer);;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @Override
    public List<NoticeBean> getAll() {
        return this.noticeDao.findAll();
    }
}
